import { authLogin } from '@/api/auth.js';

export function getUserInfo() {
	uni.getUserProfile({
		desc: '用户完善个人信息',
		success: (res) => {
			getProvider();
			uni.setStorageSync('userInfo', res.userInfo);
		},
		fail: () => {
			uni.showToast({
				title: '授权失败',
				icon: 'error',
				duration: 2000
			})
		}
	})
}

function getProvider() {
	uni.getProvider({
		desc: '获取终端设备字符',
		service: 'oauth',
		success: function(res) {
			let provider = res.provider[0];
			uni.setStorageSync('provider', provider);
			login(provider);
		}
	});
}

function login(provider) {
	uni.login({
		provider,
		success: (res) => {
			let code = res.code;
			let userInfo = uni.getStorageSync("userInfo");
			let authLoginObj = Object.assign(userInfo,{provider,code});
			authLogin(authLoginObj);
		}
	})
}